#pragma once

#define AN_CHANNEL_SINGLE 1
#define AN_CHANNEL_RGB    3
#define AN_CHANNEL_ALPHA  4

#define AN_LASTERROR getLastAnError()

typedef unsigned char uchar;

enum AnImgColorArrange
{
	AnImgColor_R=0,
	AnImgColor_G,
	AnImgColor_B,
	AnImgColor_A
};
enum CImageColorArrange
{
	CImageColor_B=0,
	CImageColor_G,
	CImageColor_R,
	CImageColor_A
};
/*format*/
enum AnImageFormat
{
	UNKNOWN = 0,
	BMP = 1,
	JPG = 2,
	PNG = 3,
};

/*color*/
struct AnColor
{
	uchar R;
	uchar G;
	uchar B;
	uchar A;

	AnColor()
	{
		R = 0;
		G = 0;
		B = 0;
		A = 0;
	}

	AnColor(uchar r, uchar g, uchar b, uchar a)
	{
		R = r;
		G = g;
		B = b;
		A = a;
	}

	AnColor(uchar allColor)
	{
		R = allColor;
		G = allColor;
		B = allColor;
		A = allColor;
	}
	
	AnColor(const AnColor &color)
	{
		R = color.R;
		G = color.G;
		B = color.B;
		A = color.A;
	}

};

/*buffer*/
typedef struct _AnImagebuffer
{
	int  width = -1;
	int  height = -1;
	int  channel = -1;
	int  dataSize = -1;
	bool isValid = false;
	AnImageFormat format = AnImageFormat::UNKNOWN;
	char *buffer = nullptr;
} AnImagebuffer;

/*function*/
namespace An
{
	bool CreateImagebuffer(int width, int height, int channel, AnImageFormat format,AnImagebuffer **buffer);

	bool LoadImageBuffer(const char *path, AnImagebuffer **buffer);

	bool SaveImageBuffer(const char *path, AnImagebuffer *buffer);

	bool CopyImageBuffer(AnImagebuffer **Dstbuffer,AnImagebuffer *Srcbuffer);

	bool GrayImgeBuffer(AnImagebuffer **Dstbuffer, AnImagebuffer *Srcbuffer);

	bool ThresholdImgeBuffer(AnImagebuffer **buffer, int thresholdValueMin, int thresholdvalueMax);

	bool ShowImageBuffer(AnImagebuffer *buffer,CWnd *cWnd, bool autoAdapt=false);

	bool ReleaseImgbuffer(AnImagebuffer **buffer);

	bool getImagebufferPixColor(int widthPos,int heightPos,AnImagebuffer *buffer,AnColor &color);

	bool setImagebufferPixColor(int widthPos,int heightPos,AnImagebuffer *buffer,AnColor color);

	AnImageFormat getImageFormat(const char *path);
}
/*error*/
DWORD         getLastAnError();